{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from py3gpp import *\n",
    "import numpy as np\n",
    "import sys\n",
    "sys.path.append(\"../ai-sdr\")\n",
    "\n",
    "from NR.ssb import SSBInfo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "ssb = SSBInfo().cellid(472).set_ibar(5).set_init_slot(0)\n",
    "mu = 1\n",
    "fs = 15360000\n",
    "scs = 15 * 2**(mu)\n",
    "nSlot = 0\n",
    "rxSampleRate = fs\n",
    "nrb = 20\n",
    "carrier = nrCarrierConfig(NSizeGrid = 20, SubcarrierSpacing = 15 * 2**mu)\n",
    "wv = ssb.ref3(carrier, fs)\n",
    "CyclicPrefix = \"normal\"\n",
    "Nfft = None\n",
    "SampleRate = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "if Nfft == None:\n",
    "    if SampleRate == None:\n",
    "        Nfft = nrOFDMInfo(nrb=nrb, scs=scs)[\"Nfft\"]\n",
    "        SampleRate = int(Nfft * scs * 1000)\n",
    "    else:\n",
    "        Nfft = int(SampleRate // scs // 1000)\n",
    "mu = (scs // 15) - 1\n",
    "if CyclicPrefix == \"normal\":\n",
    "    N_cp1 = int(((144) * 2 ** (-mu) + 16) * (SampleRate / 30720000))\n",
    "    N_cp2 = int((144 * 2 ** (-mu)) * (SampleRate / 30720000))\n",
    "else:\n",
    "    N_cp1 = int((512 * 2 ** (-mu)) * (SampleRate / 30720000))\n",
    "    N_cp2 = N_cp1\n",
    "N_cp = np.zeros(carrier.SymbolsPerSlot, dtype=int)\n",
    "for i in range(len(N_cp)):\n",
    "    N_cp[i] = N_cp1 if i == 0 or i == 7 * 2 ** (mu) else N_cp2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "rxGrid = nrOFDMDemodulate(waveform = wv, \n",
    "                                  nrb = nrb, scs = scs, initialNSlot = nSlot, \n",
    "                                  SampleRate=rxSampleRate, CyclicPrefixFraction=0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "rxGrid = rxGrid[:,:4]\n",
    "rxGrid /= np.max((rxGrid.real.max(), rxGrid.imag.max()))\n",
    "sssIndices = nrSSSIndices()\n",
    "sssRx = nrExtractResources(sssIndices, rxGrid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "sssEst = np.zeros(336)\n",
    "nid2 = 1\n",
    "for NID1 in range(335):\n",
    "    ncellid = (3*NID1) + nid2\n",
    "    sssRef = nrSSS(ncellid)\n",
    "    sssEst[NID1] = np.abs(np.vdot(sssRx, sssRef))\n",
    "detected_NID1 = np.argmax(sssEst)\n",
    "detected_NID = detected_NID1*3 + nid2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "dmrsIndices = nrPBCHDMRSIndices(detected_NID, style='matlab')\n",
    "xcorrPBCHDMRS = np.empty(7)\n",
    "for ibar_SSB in range(7):\n",
    "    PBCHDMRS = nrPBCHDMRS(detected_NID, ibar_SSB)\n",
    "    xcorrPBCHDMRS[ibar_SSB] = np.abs(np.vdot(nrExtractResources(dmrsIndices, rxGrid), PBCHDMRS))\n",
    "ibar_SSB = np.argmax(np.abs(xcorrPBCHDMRS))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "472 5\n"
     ]
    }
   ],
   "source": [
    "print(detected_NID,ibar_SSB)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n"
     ]
    }
   ],
   "source": [
    "print(carrier.SymbolsPerSlot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(127+5.74736295316471e-13j)\n"
     ]
    }
   ],
   "source": [
    "ncellid = 472\n",
    "sssRef = nrSSS(ncellid)\n",
    "sssEst = np.vdot(sssRx, sssRef)\n",
    "print(sssEst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(127+2.483550985187982e-12j)\n"
     ]
    }
   ],
   "source": [
    "nid2 = 1\n",
    "pssRef = nrPSS(nid2)\n",
    "pssRx = nrExtractResources(nrPSSIndices(),rxGrid)\n",
    "pssEst = np.vdot(pssRx, pssRef)\n",
    "print(pssEst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(143.99999999999997+6.261657858885883e-13j)\n"
     ]
    }
   ],
   "source": [
    "dmrsIndices = nrPBCHDMRSIndices(ncellid, style='matlab')\n",
    "PBCHDMRS = nrPBCHDMRS(ncellid, 5)\n",
    "drmsRx = nrExtractResources(dmrsIndices, rxGrid)\n",
    "drmsEst = np.vdot(drmsRx, PBCHDMRS)\n",
    "print(drmsEst)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.11.3 ('venv': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "160761bc1a904adbbd606c3a9b02244365b1a540f860a2c66cbc5cbd24c54900"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
